<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-16 20:57:17
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-03-01 11:56:15
 * ------------------------------------------------ *
 * 订单管理                                          *
 * ------------------------------------------------ */
namespace Admin\Model;


class OrdersModel extends CommonModel{

// 订单详情
	 public function getDetail(){

	 	$m = M('orders');
	 	$id = (int)I('id',1);

		$sql = "select o.*,s.shopName from __PREFIX__orders o
	 	         left join __PREFIX__shops s on o.shopId=s.shopId 
	 	         where o.orderFlag=1 and o.orderId=".$id;
	 	$result = $this->query($sql);
		$rs = $result[0];

		//获取用户详细地址
		$sql = 'select a1.name areaName1,a2.name areaName2,a3.name areaName3 from __PREFIX__orders c 
		        left join __PREFIX__region a1 on a1.id=c.areaId1 
		        left join __PREFIX__region a2 on a2.id=c.areaId2
		        left join __PREFIX__region a3 on a3.id=c.areaId3';
		$result = $this->query($sql);    
		$cRs = $result[0];
		//return $cRs;
		$rs['userAddress'] = $cRs['areaName1'].$cRs['areaName2'].$cRs['areaName3'].$rs['userAddress'];
		//获取日志信息
		$m = M('log_orders');
		$sql = "select lo.*,u.loginName,u.userType,s.shopName from __PREFIX__log_orders lo
		         left join __PREFIX__users u on lo.logUserId = u.userId
		         left join __PREFIX__shops s on u.userType!=0 and s.userId=u.userId
		         where orderId=".$id;
		$rs['log'] = $this->query($sql);
		//获取相关商品
		$sql = "select og.*,g.goodsImg,g.goodsName,g.goodsId from __PREFIX__order_goods og
			        left join __PREFIX__goods g on og.goodsId=g.goodsId
			        where og.orderId = ".$id;
		$rs['goodslist'] = $this->query($sql);
		
		return $rs;
	 }

// 获取退款信息

	 public function getRefundDetail(){
	 	$m = M('orders');
	 	$rs= $m->where('isRefund=0 and payType=1 and isPay=1 and orderFlag=1 and orderStatus in (-1,-4,-6,-7) and orderId='.(int)I('id'))->find();
	 	return $rs;
	 }

// 订单列表

     public function getOrders(){
        $m = M('goods');

        $shopName = I('shopName');
     	$orderNo = I('orderNo');

     	$search=I('search');
     	if(!empty($search)){
	        if(is_numeric($search)){
	        	$orderNo =$search;
	        }else{
	        	$shopName=$search;
	        }
	    }
     	$pageSize = (int)I('pageSize',0);

     	$orderStatus = (int)I('orderStatus',-10);
	 	$sql = "select o.orderId,o.orderNo,o.totalMoney,o.orderStatus,o.deliverMoney,o.payType,o.createTime,s.shopName,o.userName from __PREFIX__orders o
	 	         left join __PREFIX__shops s on o.shopId=s.shopId  where o.orderFlag=1 ";
	 	
	 	if($shopName!='')$sql.=" and (s.shopName like '%".$shopName."%' or s.shopSn like '%".$shopName."%')";
	 	if($orderNo!='')$sql.=" and o.orderNo like '%".$orderNo."%' ";
	 	if($orderStatus!=-10 && $orderStatus!=-9)$sql.=" and o.orderStatus=".$orderStatus;
	 	if($orderStatus==-9)$sql.=" and o.orderStatus in(-6,-7)";
	 	$sql.=" order by orderId desc";   
		$result = $this->PQ($sql,0,$pageSize);
		//涉及的订单及商品
		if(count($result['list'])>0){
			$orderIds = array();
			foreach ($result['list'] as $key => $v){
				$orderIds[] = $v['orderId'];
			}
			$sql = "select og.orderId,og.goodsImg,og.goodsName,og.goodsId from __PREFIX__order_goods og
			        where og.orderId in(".implode(',',$orderIds).")";
		    $rs = $this->query($sql);
		    $goodslist = array();
		    foreach ($rs as $key => $v){
		    	$goodslist[$v['orderId']][] = $v;
		    }
		    foreach ($result['list'] as $key => $v){
		    	$result['list'][$key]['goodslist'] = $goodslist[$v['orderId']];
		    }
		}
		return $result;
	 }

// 退款列表

     public function getRefund(){
        $m = M('goods');
        $shopName = I('shopName');
     	$orderNo = I('orderNo');
     	$pageSize = (int)I('pageSize',0);
     	$isRefund = (int)I('isRefund',-1);
	 	$sql = "select o.orderId,o.orderNo,o.totalMoney,o.orderStatus,o.isRefund,o.deliverMoney,o.payType,o.createTime,s.shopName,o.userName from __PREFIX__orders o
	 	         left join __PREFIX__shops s on o.shopId=s.shopId  where o.orderFlag=1 and o.orderStatus in (-1,-4,-6,-7) and payType=1 and isPay=1 ";
	 	
	 	if($isRefund>-1)$sql.=" and o.isRefund=".$isRefund;
	 	if($shopName!='')$sql.=" and (s.shopName like '%".$shopName."%' or s.shopSn like '%".$shopName."%')";
	 	if($orderNo!='')$sql.=" and o.orderNo like '%".$orderNo."%' ";
	 	$sql.=" order by orderId desc";  
		$result = $this->PQ($sql,0,$pageSize);
		//涉及的订单及商品
		if(count($result['list'])>0){
			$orderIds = array();
			foreach ($result['list'] as $key => $v){
				$orderIds[] = $v['orderId'];
			}
			$sql = "select og.orderId,og.goodsImg,og.goodsName,og.goodsId from __PREFIX__order_goods og
			        where og.orderId in(".implode(',',$orderIds).")";
		    $rs = $this->query($sql);
		    $goodslist = array();
		    foreach ($rs as $key => $v){
		    	$goodslist[$v['orderId']][] = $v;
		    }
		    foreach ($result['list'] as $key => $v){
		    	$result['list'][$key]['goodslist'] = $goodslist[$v['orderId']];
		    }
		}
		return $result;
	 }

// 退款处理

	 public function setRefund(){
	 	$m = M('orders');
	 	$rs = $m->where('isRefund=0 and orderFlag=1 and orderStatus in (-1,-4,-6,-7) and payType=1 and isPay=1 and orderId='.I('id'))->find();
	 	if($rs['orderId']!=''){
	 		$data = array();
	 		$data['isRefund'] = 1;
	 		$data['refundRemark'] = I('content');
	 	    $rss = $m->where("orderId=".(int)I('id',0))->save($data);
			if(false !== $rs){
				return true;
			}else{
				return "other";//已退款
			}
	 	}
	 	return false;
	 }
}